import {loginRequest,getUserInfoRequest,exitLoginRequest} from '@/api/index'

const state ={
    token:localStorage.getItem('USER_TOKEN') || '',
    nickName:''
};
const mutations = {
    SET_LOGIN_STATE(state,value){
        state.token = value
    },
    SET_USERNAME_STATE(state,userName){
        state.nickName = userName
    },
    SET_LOGINOUT_STATE(state){
        state.token = '',
        state.nickName = ''
        localStorage.removeItem('USER_TOKEN')
    }
};
const actions = {
   async getUserLogin({commit},{phone,password}){
     const re =  await loginRequest({phone,password})
     console.log(re.token);
        commit('SET_LOGIN_STATE',re.token)
        // 获取到token之后需要让token持久化
        localStorage.setItem('USER_TOKEN',re.token)
    },
    async getUserMsg ({commit}){
        const re = await getUserInfoRequest()
        commit('SET_USERNAME_STATE',re.nickName)
    },
    // 退出登录
    async getloginOut({commit}){
        await exitLoginRequest()
        commit('SET_LOGINOUT_STATE')
    }

};
const getters = {

};

export default {
    namespaced:true,
    state,
    mutations,
    actions,
    getters
}